/**
 * @(#) SourceSet.java;
 * <p/>
 * Created on Jun 20, 2008
 * AUTHOR    ** Danil Glinenko
 * EMAIL     ** dglinenko@gmail.com
 * <p/>
 *
 * Copyright (C) 2008 Danil Glinenko. All rights reserved.
 */

package jacky.lanlan.song.test.db;

import org.ho.yaml.Yaml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class SourceSet {
	private final Map<String, Source> sources = new ConcurrentHashMap<String, Source>();

	private String ymlCfgFilePath;
	
	SourceSet(final InputStream dsInputStream) {
		if (dsInputStream == null) { throw new IllegalArgumentException(
				"InputStream with source definitions cannot be null"); }
		final Map<String,Map<String, String>> dbSources = (Map) Yaml.load(dsInputStream);

		for (final String sourceName : dbSources.keySet()) {
			final Map<String, String> sourceData = dbSources.get(sourceName);
			final String driver = sourceData.get("driver");
			final String url = sourceData.get("url");
			final String username = sourceData.get("username");
			final String password = sourceData.get("password");

			final Source source = new Source(sourceName, driver, url, username, password);
			this.sources.put(sourceName, source);
		}
	}

	void setYmlCfgFilePath(String ymlCfgFilePath) {
		this.ymlCfgFilePath = ymlCfgFilePath;
	}
	
	public String getYmlCfgFilePath() {
		return this.ymlCfgFilePath;
	}
	
	public Source getSource(final String sourceName) {
		final Source source = sources.get(sourceName);
		if (source == null) { throw new RuntimeException("\"" + sourceName + "\"" + " is not initialized."); }
		return source;
	}
	
	public List<Source> getSourceList(){
		return new ArrayList<Source>(this.sources.values());
	}
	
}
